package cn.edu.sdjzu.xg.eagle.dao;

import cn.edu.sdjzu.xg.eagle.entity.Teacher;
import cn.edu.sdjzu.xg.eagle.entity.User;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.sql.*;
import java.time.LocalDateTime;

public class UserDaoImpl implements UserDaoInterface {
    // 单例实例（volatile 保证可见性）
    private static volatile UserDaoInterface instance = null;

    // 私有构造函数
    public UserDaoImpl() {}

    // 双检锁单例模式
    public static UserDaoInterface getInstance() {
        if (instance == null) {
            synchronized (UserDaoImpl.class) {
                if (instance == null) {
                    instance = new UserDaoImpl();
                    System.out.println("Create UserDaoImpl Object");
                }
            }
        }
        return instance;
    }

    @Override
    public User find(Long id) throws SQLException {
        User desiredUser = null;
        String sql = "SELECT * FROM bysj.user WHERE id = ?";

        try (Connection connection = JdbcHelper.getConn();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setLong(1, id);

            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    String no = rs.getString("no");
                    String username = rs.getString("username");
                    Long teacherId = rs.getLong("teacher_id");

                    Teacher teacher = new Teacher();
                    teacher.setId(teacherId);
                    desiredUser = new User(teacherId, no, username, teacher);
                }
            }
        } // 自动关闭 Connection、PreparedStatement、ResultSet
        return desiredUser;
    }

    @Override
    public boolean add(Connection connection, User user) throws SQLException {
        String sql = "INSERT INTO user (no, username, teacher_id) VALUES (?, ?, ?)";

        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, user.getNo());
            pstmt.setString(2, user.getUsername());
            pstmt.setLong(3, user.getTeacher().getId());

            int rowAffected = pstmt.executeUpdate();
            return rowAffected > 0;
        }
    }

    @Override
    public boolean delete(Connection connection, Long id) throws SQLException {
        String sql = "DELETE FROM user WHERE id = ?";

        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setLong(1, id);
            int rowAffected = pstmt.executeUpdate();
            return rowAffected > 0;
        }
    }

    @Override
    public void login(Connection connection, User user) throws SQLException {
        String sql = "SELECT id FROM user WHERE username = ? AND password = ?";

        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());

            try (ResultSet resultSet = pstmt.executeQuery()) {
                if (resultSet.next()) {
                    user.setId(resultSet.getLong("id"));
                }
            }
        }
    }
}